<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/7/2
 * Time: 17:38
 */

namespace app\supplier\controller;

use app\company\model\CompanyGoodsRule;
use app\company\model\CompanyOrderStorage;
use app\mini\model\StoreOrderDetail;
use think\facade\Request;
use think\Response;

class Report extends Base
{
    /**
     * 货物报表
     * @return Response
     */
    public function goods(): Response
    {
        $param = $this->request->param();
        //首先查询出所有经销商商品规格ID
        $ids       = CompanyGoodsRule::gs($this->company_id)->goodsId($param['goods_id'])->supplierId($this->supplier_id)->distinct(true)->column("goods_rule_id");
        $sku_num   = count($ids);//sku数量
        $stock_num = CompanyOrderStorage::gs($this->company_id)->goodsId($param['goods_id'])->whereIn('goods_rule_id', $ids)->sum('quantity');//库存量
        $sale_num  = StoreOrderDetail::gs($this->company_id)->whereIn('goods_rule_id', $ids)->goodsId($param['goods_id'])->sum('quantity');//销量
        $sql       = CompanyGoodsRule::gs($this->company_id)->goodsId($param['goods_id'])->supplierId($this->supplier_id);
        $count     = $sql->count();
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $list = $sql->field("id,goods_id,goods_rule_id")->select()->each(function ($item) {
            $item->goods      = $item->goods()->withTrashed()->field("name,unit,no")->find();//商品码/商品名称/单位
            $item->goods_rule = $item->goodsRule()->withTrashed()->field("spec_name")->find();//规格
            $item->sale_num   = StoreOrderDetail::gs($this->company_id)->goodsRuleId($item->goods_rule_id)->sum('quantity');//销量
            $item->stock_num  = CompanyOrderStorage::gs($this->company_id)->goodsRuleId($item->goods_rule_id)->sum('quantity');//库存
        });
        return $this->success('请求成功', compact('list', 'count', 'sale_num', 'stock_num', 'sku_num'));
    }

    /**
     * @param Request $request
     * @return void
     */
    public function goodsDetail()
    {
        //仓库名称 / 库位码/商品预警(天)/保质期(天)/销量
        $param = $this->request->param();

    }
}